package zj.boxuegu.administrator.myapplication.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import zj.boxuegu.administrator.myapplication.R;

public class ZhuCeActivity extends AppCompatActivity {
    EditText mUsername1,mPassword1,mPassword3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhu_ce);
        mUsername1= (EditText) findViewById(R.id.mUsername1);
        mPassword1= (EditText) findViewById(R.id.mPassword1);
        mPassword3= (EditText) findViewById(R.id.mPassword3);

    }
    public void fanhui1 (View view){
        finish();
    }
    public void zhuce(View view) {
        if(TextUtils.isEmpty(mUsername1.getText().toString())){
            Toast.makeText(this,"用户名不能为空",Toast.LENGTH_SHORT).show();

        }
        else if(TextUtils.isEmpty(mPassword1.getText().toString())){
            Toast.makeText(this,"密码不能为空",Toast.LENGTH_SHORT).show();
        }
            else if(!mPassword1.getText().toString().equals(mPassword3.getText().toString())) {

                Toast.makeText(this, "两次密码请输入一致", Toast.LENGTH_SHORT).show();
            }
            else {
                finish();
            Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
            }

        RequestParams params = new RequestParams("http://1633.applinzi.com/public/?s=App.User.Register");

        params.addQueryStringParameter("username", mUsername1.getText().toString());
        params.addQueryStringParameter(" password", mPassword1.getText().toString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {


                Gson gson =new  Gson();

                LoginModel loginModel =gson.fromJson(result,LoginModel.class);



                if(loginModel.code==200){
                    finish();

                }else{
                 Toast.makeText(ZhuCeActivity.this,"",Toast.LENGTH_SHORT).show();

                }
               Log.i("LoginActivity","onSuccess loginModel code"+loginModel.code);

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();



            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFinished() {

            }
        });
    }
}
